<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/cookie.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        a {
            color: inherit;
            font-size: 13px;
            text-decoration: none;
        }

        .w {
            width: 1226px;
            margin: 0 auto;
        }

        .header {
            height: 45px;
            line-height: 45px;
            /* background-color: grey; */
        }

        .header .site-nav-login>div {
            display: none;
        }

        .header .site-nav-login .show {
            display: block;
        }

        .showGrade table {
            width: 100%;
            table-layout: fixed;
            border: 1px solid #666;
            border-spacing: 0;
            border-collapse: collapse;
            line-height: 36px;
            text-align: center;

        }

        .showGrade table th,
        .showGrade table td {
            border: 1px solid #666;
        }

        .shadow {
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            position: fixed;
            left: 0;
            top: 0;
            display: none;
        }

        .shadow form {
            padding: 20px;
            background-color: #fff;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }

        .shadow form p+p {
            margin-top: 8px;
        }
    </style>
</head>

<body>
    <div class="header w">
        <div class="site-nav-login">
            <div class="nav-login-sign show">
                <a href="./login.html">亲,请登录</a>
                <a href="./register.html">账号注册</a>
            </div>
            <div class="nav-login-user">
                <a href="javascript:;" class="loginTips">欢迎,xxx</a>
                <a href="javascript:;" class="exit">退出</a>
            </div>
        </div>
    </div>
    <div class="showGrade w">
        <table>
            <thead>
                <tr>
                    <th>编号</th>
                    <th>姓名</th>
                    <th>班级</th>
                    <th>语文</th>
                    <th>数学</th>
                    <th>英语</th>
                    <th>总分</th>
                    <th>编辑</th>
                    <th>删除</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>张三</td>
                    <td>2108</td>
                    <td>55</td>
                    <td>66</td>
                    <td>77</td>
                    <td>198</td>
                    <td><a href="javascript:;" class="edit">编辑</a></td>
                    <td><a href="javascript:;" class="edit">删除</a></td>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="shadow">
        <form onsubmit="return false">
            <p><label>编号:<input type="text" name="id" disabled></label></p>
            <p><label>姓名:<input type="text" name="name" disabled></label></p>
            <p><label>班级:<input type="text" name="class" disabled></label></p>
            <p><label>语文:<input type="text" name="chinese"></label></p>
            <p><label>数学:<input type="text" name="math"></label></p>
            <p><label>英语:<input type="text" name="english"></label></p>
            <p>
                <input type="submit" class="save" value="保存">
                <input type="reset" class="cancel" value="取消">
            </p>
        </form>
    </div>
</body>
<script>
    var siteNavLogin = document.getElementsByClassName("site-nav-login")[0];
    var sitNavList = siteNavLogin.children;
    var loginTips = document.querySelector(".loginTips");
    var exit = document.querySelector(".exit");
    var showGrade = document.querySelector(".showGrade");
    var tbody = document.querySelector(".showGrade tbody");

    // 标识用户身份
    var cookie = document.cookie;
    if (cookie) {
        var lgc = getCookie("lgc");
        if (lgc) {
            for (var i = 0; i < sitNavList.length; i++) {
                sitNavList[i].classList.remove("show");
            }
            sitNavList[1].classList.add("show");
            loginTips.textContent = `欢迎你,${lgc}`;

            exit.onclick = function () {
                deleteCookie("lgc");
                location.reload();
            }

        }
    }


    var xhr = new XMLHttpRequest();

    xhr.open("get", "../php/searchAllGrade.php", true);

    xhr.send();

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var result = xhr.responseText;
            console.log(result);
            result = JSON.parse(result);
            console.log(result);

            var { status, msg, list } = result;

            if (status) {
                if (list.length) {

                    var html = "";
                    list.forEach(({ id, name, class: _class, chinese, math, english, total }) => {
                        html += `<tr  data-id="${id}">
                            <td>${id}</td>
                            <td>${name}</td>
                            <td>${_class}</td>
                            <td>${chinese}</td>
                            <td>${math}</td>
                            <td>${english}</td>
                            <td>${total}</td>
                            <td><a href="javascript:;" class="edit">编辑</a></td>
                            <td><a href="javascript:;" class="delete" >删除</a></td>
                        </tr>`;
                    })
                    tbody.innerHTML = html;

                } else {
                    showGrade.textContent = "暂无数据";
                }
            } else {
                alert(msg);
            }



        }
    }


    showGrade.onclick = function (e) {
        var e = e || window.event;
        var target = e.target || e.srcElement;

        if (target.className == "delete") {
            if (confirm("是否删除该成绩")) {
                var tr = target.parentElement.parentElement;
                // tr.remove();
                var id = tr.getAttribute("data-id");
                console.log(id);
                var xhr = new XMLHttpRequest();

                xhr.open("get", "../php/deleteGradeById.php?id=" + id, true);

                xhr.send();

                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        var result = xhr.responseText;
                        console.log(result);
                        result = JSON.parse(result);
                        console.log(result);

                        var { status, msg } = result;
                        if (status) {
                            tr.remove();
                        } else {
                            alert(msg);
                        }

                    }
                }
            }
        }

    }









</script>

</html>